/**
 * @link https://leetcode.cn/problems/add-binary/?envType=study-plan-v2&envId=top-interview-150
 */
function addBinary(a: string, b: string): string {
  let tmp = 0;

  let ans = "";

  let end1 = a.length - 1;
  let end2 = b.length - 1;

  while (end1 >= 0 && end2 >= 0) {
    console.log(ans, end1, end2);

    if (Number(a[end1]) + Number(b[end2]) + tmp === 0) {
      ans += "0";
      tmp = 0;
    } else if (Number(a[end1]) + Number(b[end2]) + tmp === 1) {
      ans += "1";
      tmp = 0;
    } else if (Number(a[end1]) + Number(b[end2]) + tmp === 2) {
      ans += "0";
      tmp = 1;
    } else if (Number(a[end1]) + Number(b[end2]) + tmp === 3) {
      ans += "1";
      tmp = 1;
    } else {
    }
    end1 -= 1;
    end2 -= 1;
  }

  while (end1 >= 0) {
    if (Number(a[end1]) + tmp === 0) {
      ans += "0";
      tmp = 0;
    } else if (Number(a[end1]) + tmp === 1) {
      ans += "1";
      tmp = 0;
    } else if (Number(a[end1]) + tmp === 2) {
      ans += "0";
      tmp = 1;
    }
    end1 -= 1;
  }

  while (end2 >= 0) {
    if (Number(b[end2]) + tmp === 0) {
      ans += "0";
      tmp = 0;
    } else if (Number(b[end2]) + tmp === 1) {
      ans += "1";
      tmp = 0;
    } else if (Number(b[end2]) + tmp === 2) {
      ans += "0";
      tmp = 1;
    }
    end2 -= 1;
  }

  if (tmp) ans += "1";

  console.log(ans.split("").reverse().join(""));

  return ans.split("").reverse().join("");
}
/**
 * @link https://leetcode.cn/problems/single-number-ii/
 */
function singleNumberII(nums: number[]): number {
  let ans = 0;
  for (let i = 0; i < 32; i++) {
    let total = 0;
    for (let num of nums) {
      total += (num >> i) & 1;
    }

    if (total % 3 != 0) {
      ans |= 1 << i;
    }
    console.log(total, ans);
  }
  console.log(ans);

  return ans;
}

/**
 * @link https://leetcode.cn/problems/single-number/?envType=study-plan-v2&envId=top-interview-150
 */
function singleNumber(nums: number[]): number {
  let ans = 0;
  for (let i = 0; i < 32; i++) {
    let total = 0;
    for (let num of nums) {
      total += (num >> i) & 1;
    }
    if (total % 2 != 0) {
      ans |= 1 << i;
    }
  }

  return ans;
}

/**
 * @link https://leetcode.cn/problems/reverse-bits/?envType=study-plan-v2&envId=top-interview-150
 */
function reverseBits(n: number): number {
  let rev = 0;
  for (let i = 0; i < 32 && n > 0; ++i) {
    rev |= (n & 1) << (31 - i);
    n = n >>> 1;
    console.log(n);
  }
  return rev >>> 0;
}

/**
 * @link https://leetcode.cn/problems/number-of-1-bits/?envType=study-plan-v2&envId=top-interview-150
 */
function hammingWeight(n: number): number {
  return 0;
}

/**
 * @link https://leetcode.cn/problems/bitwise-and-of-numbers-range/?envType=study-plan-v2&envId=top-interview-150
 */
function rangeBitwiseAnd(left: number, right: number): number {
  let ans = 0;
  for (let i = 0; i < 32; i++) {
    let tmp = 1;
    for (let j = left; j <= right; j++) {
      tmp &= (j >> i) & 1;
    }
    if (tmp != 0) ans |= 1 << i;
  }
  console.log(ans);

  return 0;
}

export default {
  addBinary,
  singleNumber,
  singleNumberII,
  reverseBits,
  hammingWeight,
  rangeBitwiseAnd,
};
